package com.sw.youvote.server.persistentobjects;

import java.util.logging.Logger;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

import com.sw.youvote.server.serverobjects.ImageBean;
import com.sw.youvote.shared.objects.QuizBean;

public class ImageJdoDAO implements ImageDAO {

	private static final Logger log = Logger.getLogger(ImageJdoDAO.class
			.getName());


	/**
	 * El metodo addImage devuelve la clave del objeto creado
	 */
	@Override
	public String addImage(ImageBean image) {
		PersistenceManager pm = WocomPersistenceManager.getPersistenceManagerFactory()
				.getPersistenceManager();

		String key = null;
		ImageBean newimageBean =null;
		log.info("addImage::Bean para insertar =" + image);
		try {
			 newimageBean =pm.makePersistent(image);
			 log.info("addImage::Bean tras la creacion =" + newimageBean);
		} finally {
			pm.close();
		}
		
		if (newimageBean != null) {
			key = newimageBean.getId();
			
		}
		
		return key;

	}

	@Override
	public ImageBean getImage(String id) {
		ImageBean result = null;
		PersistenceManager pm = WocomPersistenceManager.getPersistenceManagerFactory()
		.getPersistenceManager();

		if (id != null && !id.equals("")) {
			result = pm.getObjectById(ImageBean.class, id);
			log.info("Imagen obtenida =" + result);
		} else {
			log.info("Identificador de imagen incorrecto");
		}
		return result;
	}

} // end ImageJdoDAO class
